package com.example.library_common.networking.callback;

import android.os.Handler;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.library_common.bean.CommonBean;
import com.example.library_common.global.ConfigKeys;
import com.example.library_common.global.Latte;
import com.example.library_common.loader.LatteLoader;

import java.lang.reflect.Type;

import ikidou.reflect.TypeBuilder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public final class RequestCallbacks implements Callback<String> {

    private final IRequest REQUEST;
    private final ISuccess SUCCESS;
    private final IFailure FAILURE;
    private final IError ERROR;
    //    private final LoaderStyle LOADER_STYLE;
    private static final Handler HANDLER = Latte.getHandler();

    public RequestCallbacks(IRequest request, ISuccess success, IFailure failure, IError error) {
        this.REQUEST = request;
        this.SUCCESS = success;
        this.FAILURE = failure;
        this.ERROR = error;
//        this.LOADER_STYLE = style;
    }

    @Override
    public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
        if (response.isSuccessful()) {
            if (call.isExecuted()) {
                try {


                    Type build = TypeBuilder.newInstance(CommonBean.class)
                            .beginSubType(String.class)
                            .endSubType().build();
                    CommonBean<String> parse = JSON.parseObject(response.body(), build);
                    if (parse != null && parse.getCode() == CommonBean.SUCCEED) {
                        if (SUCCESS != null) {
                            SUCCESS.onSuccess(parse.getData());
                        }
                    } else {
                        if (ERROR != null) {
                            String errorMsg ="数据异常";
                            if (parse!=null){
                                  errorMsg = parseErrorMsg(parse.getData());
                                if (TextUtils.isEmpty(errorMsg)){
                                    errorMsg=parse.getMessage();
                                }
                            }

                            String msg = errorMsg;
                            int code = parse != null ? parse.getCode() : 30000;
                            ERROR.onError(code, msg);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (ERROR != null) {
                        ERROR.onError(3000, e.getMessage());
                    }
                }

            }
        } else {
            if (ERROR != null) {
                ERROR.onError(response.code(), response.message());
            }
        }

        onRequestFinish();
    }

    private String parseErrorMsg(String dataString) {
        Object parse = JSON.parse(dataString);
        String errMsg = "数据异常";
        if (parse instanceof JSONObject) {
            errMsg = ((JSONObject) parse).getString("errmsg");
        }
        return errMsg;
    }

    @Override
    public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
        if (FAILURE != null) {
            FAILURE.onFailure();
        }
        if (REQUEST != null) {
            REQUEST.onRequestEnd();
        }

        onRequestFinish();
    }

    private void onRequestFinish() {
        final long delayed = Latte.getConfiguration(ConfigKeys.LOADER_DELAYED);
//        if (LOADER_STYLE != null) {
        HANDLER.postDelayed(LatteLoader::stopLoading, delayed);
//        }
    }
}